package com.fruit.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;



public class CommonUtil {

	public static String getNonceStr() {
		Random random = new Random();
		return MD5Util.MD5Encode(String.valueOf(random.nextInt(10000)),"UTF-8");
	}

	public static String getTimeStamp() {
		return String.valueOf(System.currentTimeMillis() / 1000);
	}
	
	public static String map2xmlBody(Map<String, String> vo, String rootElement) {
		Element body = DocumentHelper.createElement(rootElement);
		buildMap2xmlBody(body, vo);
		return body.asXML();
	}

	private static void buildMap2xmlBody(Element body, Map<String, String> vo) {
		if (vo != null) {
			Iterator<String> it = vo.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				if (StringUtils.isNotEmpty(key)) {
					Element element = DocumentHelper.createElement(key);
					element.add(DocumentHelper.createCDATA(vo.get(key)));

					body.add(element);
				}
			}
		}
	}
	
	public static Map<String, String> xmlBody2map(String xml, String rootElement) {
		Document doc;
		Map<String, String> vo = null;
		try {
			doc = DocumentHelper.parseText(xml);
			Element body = (Element) doc.selectSingleNode(rootElement);
			vo = buildXmlBody2map(body);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return vo;
	}

	private static Map<String, String> buildXmlBody2map(Element body) {
		Map<String, String> vo = new TreeMap<String, String>();
		if (body != null) {
			List<Element> elements = body.elements();
			for (Element element : elements) {
				String key = element.getName();
				if (StringUtils.isNotEmpty(key)) {
					String text = element.getText().trim();
					vo.put(key, text);
				}
			}
		}
		return vo;
	}
	
	public static String encode(String str, String en){
		if(StringUtils.isNotEmpty(str)){
		try {
			return URLEncoder.encode(str, en);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		}else{
			return "";
		}
	}
	
	public static String decode(String str, String en){
		if(StringUtils.isNotEmpty(str)){
			try {
				return URLDecoder.decode(str, en);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "";
			}
		}else{
			return "";
		}

	}
	
	public static void makeDisInfo(double min, double max){
		BigDecimal minB = new BigDecimal(min).setScale(2, BigDecimal.ROUND_HALF_DOWN);
		BigDecimal maxB = new BigDecimal(max).setScale(2, BigDecimal.ROUND_HALF_DOWN);
		BigDecimal temp = minB;
		BigDecimal max8 = new BigDecimal("8");
		
		BigDecimal pointsRatio = new BigDecimal("100");
		BigDecimal points2 = new BigDecimal("2");
		BigDecimal points6 = new BigDecimal("6");
		BigDecimal points7 = new BigDecimal("7");
		BigDecimal points8 = new BigDecimal("8");
		BigDecimal points85 = new BigDecimal("8.5");
		BigDecimal points9 = new BigDecimal("9");
		
		BigDecimal offlineRatio = new BigDecimal("100");
		BigDecimal offline8 = new BigDecimal("8");
		BigDecimal offline83 = new BigDecimal("8.3");
		BigDecimal offline85 = new BigDecimal("8.5");
		BigDecimal offline88 = new BigDecimal("8.8");
		BigDecimal offline9 = new BigDecimal("9.0");
		BigDecimal offline95 = new BigDecimal("9.5");
		while(temp.compareTo(maxB)<=0){
			
			BigDecimal pRatio = new BigDecimal("0.1");
			BigDecimal cRatio = new BigDecimal("1");
			if(temp.compareTo(max8) < 0 ){
			cRatio = max8.subtract(temp).multiply(new BigDecimal("5")).setScale(0, BigDecimal.ROUND_DOWN).add(new BigDecimal("1"));
			}
			
			if(temp.compareTo(points2) >=0 && temp.compareTo(points6) <=0){
				pointsRatio = new BigDecimal("100"); 
			}else if(temp.compareTo(points6) >0 && temp.compareTo(points7) <=0){
				pointsRatio = new BigDecimal("50"); 
			}else if(temp.compareTo(points7) >0 && temp.compareTo(points8) <=0){
				pointsRatio = new BigDecimal("30"); 
			}else if(temp.compareTo(points8) >0 && temp.compareTo(points85) <=0){
				pointsRatio = new BigDecimal("10"); 
			}else if(temp.compareTo(points85) >0 && temp.compareTo(points9) <=0){
				pointsRatio = new BigDecimal("5"); 
			}else{
				pointsRatio = new BigDecimal("0"); 
			}
			
			if(temp.compareTo(offline8) <= 0){
				offlineRatio = new BigDecimal("100");
			}else if(temp.compareTo(offline8) >0 && temp.compareTo(offline83) <=0){
				offlineRatio = new BigDecimal("80");
			}else if(temp.compareTo(offline83) >0 && temp.compareTo(offline85) <=0){
				offlineRatio = new BigDecimal("65");
			}else if(temp.compareTo(offline85) >0 && temp.compareTo(offline88) <=0){
				offlineRatio = new BigDecimal("50");
			}else if(temp.compareTo(offline88) >0 && temp.compareTo(offline9) <=0){
				offlineRatio = new BigDecimal("35");
			}else if(temp.compareTo(offline9) >0 && temp.compareTo(offline95) <=0){
				offlineRatio = new BigDecimal("10");
			}
			
			
			
			StringBuffer sb = new StringBuffer();
			sb.append("insert into hltx_bonus_config(discount,p_ratio,c_ratio,points_ratio,offline_ratio)")
			.append("values(").append(temp).append(",").append(pRatio).append(",").append(cRatio).append(",").append(pointsRatio).append(",").append(offlineRatio).append(");");
			
			System.out.println(sb.toString());
			
			temp = temp.add(new BigDecimal("0.01"));
		}
		
		
	}
	
	
	public static void main(String arags[]){

		makeDisInfo(2,9.5);
	}
	
}
